
#include "../server/rpc_router.hpp"
#include "../common/dispatcher.hpp"
#include "../common/net.hpp"
#include "../common/message.hpp"
void Add(const Json::Value &req,Json::Value &rsp)
{
    int num1=req["num1"].asInt();
    int num2=req["num2"].asInt();
    rsp=num1+num2;
}
int main()
{
    auto router = std::make_shared<myrpc::server::RpcRouter>();
    std::unique_ptr<myrpc::server::ServiceDescribeFactory> desc_factory(new myrpc::server::ServiceDescribeFactory());
    desc_factory->setMethodName("Add");
    
    desc_factory->setParamDesc("num1",myrpc::server::VType::INTEGRAL);
    desc_factory->setParamDesc("num2",myrpc::server::VType::INTEGRAL);
    desc_factory->setReturnType(myrpc::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    
    router->registerMethod(desc_factory->build());

    auto cb=std::bind(&myrpc::server::RpcRouter::onRpcRequest,router.get(),std::placeholders::_1,std::placeholders::_2);
    auto dispatcher=std::make_shared<myrpc::Dispatcher>();
    dispatcher->registerHandler<myrpc::RpcRequest>(myrpc::MType::REQ_RPC,cb);

    auto server=myrpc::ServerFactory::create(9090);
    auto message_cb=std::bind(&myrpc::Dispatcher::onMessage,dispatcher.get(),std::placeholders::_1,std::placeholders::_2);
    server->setMessageCallback(message_cb);
    server->start();
    return 0;
}